package com.example.tool;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.example.tool.activtiy.DbTestActivity;
import com.example.tool.activtiy.FileToolActivity;
import com.example.tool.activtiy.MainActivity;
import com.example.tool.activtiy.NetActivity;
import com.example.tool.activtiy.SourceOriginalActivtiy;
import com.example.tool.activtiy.ThreadActivity;
import com.example.tool.activtiy.TikAiActivity;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.adapter.HomeRvAdapter;
import com.example.tool.listener.OnItemListener;
import com.example.tool.listener.PermissionListener;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.thread.DbCreatThread;
import com.example.tool.util.ConstantVale;
import com.example.tool.util.DebugLog;
import com.example.tool.util.FileUtil;
import com.example.tool.util.PermissionUtil;
import com.example.tool.util.SpUtil;
import com.example.tool.util.ThreadPoolUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.example.tool.util.ThreadPoolUtil.SingleThread;

public class HomeActivity extends BaseActivity<CommonPresent> {
    RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private HomeRvAdapter homeRvAdapter;
    private String[] strings = new String[]{"UI工具", "数据库工具", "网络工具", "文件操作工具", "线程优化"
            , "源码原理分析", "仿抖音", "FileProvide"};
    private TextView tvTitle;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {

        recyclerView = findViewById(R.id.rv_home);
        tvTitle = findViewById(R.id.tv_title);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        homeRvAdapter = new HomeRvAdapter(getData());
        tvTitle.setText("Andriod工具类");
        requestPermisson();
    }


    private void requestPermisson() {
        PermissionUtil.requestPermission(new PermissionListener() {
            @Override
            public void onGranted() {
                String tiktok = FileUtil.getInstance().getSdcardPath() + File.separator + "DCIM/tiktok";
                String path = FileUtil.getInstance().getSdcardPath() + "/okDown/demo/";
                DebugLog.d(DebugLog.KUN + " 创建抖音文件夹。。tiktok。" + tiktok);
                FileUtil.getInstance().createFileDir(tiktok);
                FileUtil.getInstance().createFileDir(path);
            }

            @Override
            public void onDenied(List<String> deniedPermissions) {

            }
        });
    }

    @Override
    protected void initData() {
        //模拟增加数据
        if (!SpUtil.getBoolean(ConstantVale.FIRST_ADD)) {
            ThreadPoolUtil.initThreadPoolUtils(SingleThread, 1).execute(new DbCreatThread());
            SpUtil.putBoolean(ConstantVale.FIRST_ADD, true);
        }
        getData();
        recyclerView.setAdapter(homeRvAdapter);
        homeRvAdapter.setOnItemListener(new OnItemListener() {
            @Override
            public void OnItemClick(int postion) {
                switch (postion) {
                    case 0:
                        startToActivity(HomeActivity.this, MainActivity.class);
                        break;
                    case 1:
                        startToActivity(HomeActivity.this, DbTestActivity.class);
                        break;
                    case 2:
                        startToActivity(HomeActivity.this, NetActivity.class);
                        break;
                    case 3:
                        startToActivity(HomeActivity.this, FileToolActivity.class);
                        break;
                    case 4:
                        startToActivity(HomeActivity.this, ThreadActivity.class);
                    case 5:
                        startToActivity(HomeActivity.this, SourceOriginalActivtiy.class);
                        break;
                    case 6:
//                        startToActivity(HomeActivity.this, TikTokActivity.class);
                        startToActivity(HomeActivity.this, TikAiActivity.class);
                        break;
                    case 7:
//                        代码适配
                        File file = new File("/storage/emulated/0/Download/dili04.jpg");
                        Intent intent = new Intent();
                        Uri uri = null;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            //主要修改就在下面3行
                            uri = FileProvider.getUriForFile(MyApplication.getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);

                        } else {
                            uri = Uri.fromFile(file);
                        }
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        intent.setDataAndType(uri, "image/*");
                        intent.setDataAndType(uri, "image/*");
                        intent.putExtra("crop", "true");
                        intent.putExtra("outputX", 80);
                        intent.putExtra("outputY", 80);
                        intent.putExtra("return-data", false);
                        startActivityForResult(intent, 1);

                        break;

                }
            }
        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected CommonPresent createPresenter() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private List<String> getData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }
        return list;
    }
}
